

export default class CCNodePool {

    private pool: cc.NodePool;

    private template: cc.Prefab;

    private name: string = ''

    // private getCount:number = 0;

    // private putCount:number = 0;

    /**
     * 
     * @param prefab 预制体
     * @param conut 初始化个数
     */
    constructor(name: string, prefab: cc.Prefab, conut: number) {
        this.name = name;
        this.pool = new cc.NodePool();
        this.template = prefab;
        for (let i = 0; i < conut; i++) {
            let obj: cc.Node = this.getNode(); // 创建节点
            this.pool.put(obj); // 通过 putInPool 接口放入对象池
        }
    }

    getName() {
        return this.name
    }

    get() {
        // let used = this.getCount - this.putCount;
        // if( used > 50){
        // cc.warn(' this.getCount is ',this.getCount,' putCount ',this.putCount," name ",this.name )
        // }
        // this.getCount ++;

        let go: cc.Node = this.pool.size() > 0 ? this.pool.get() : this.getNode();
        // cc.log(' get count ',this.pool.size(),go.name )
        return go;
    }

    getNode() {

        return cc.instantiate(this.template);

    }

    size() {
        return this.pool.size();
    }

    put(go: cc.Node) {
        // this.putCount ++;
        // cc.log(' put ',go.name)
        this.pool.put(go);
        // cc.log(' put  count ',this.pool.size(),go.name )
    }

    clear() {
        this.pool.clear();
    }

}
